﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类Base_Permit_RoleDefine。
    /// </summary>
    public class Base_Permit_RoleDefine
    {
        public Base_Permit_RoleDefine()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RoleId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Base_Permit_RoleDefine where RoleId=@RoleId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RoleId", DbType.Guid, RoleId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Base_Permit_RoleDefine(");
            strSql.Append("RoleId,RoleName,CityId,CityName,CompanyId,CompanyName,IsShow,DefRole)");

            strSql.Append(" values (");
            strSql.Append("@RoleId,@RoleName,@CityId,@CityName,@CompanyId,@CompanyName,@IsShow,@DefRole)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RoleId", DbType.Guid, model.RoleId);
            db.AddInParameter(dbCommand, "RoleName", DbType.AnsiString, model.RoleName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "IsShow", DbType.Boolean, model.IsShow);
            db.AddInParameter(dbCommand, "DefRole", DbType.Boolean, model.DefRole);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Base_Permit_RoleDefine set ");
            strSql.Append("RoleName=@RoleName,");
            strSql.Append("CityId=@CityId,");
            strSql.Append("CityName=@CityName,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("CompanyName=@CompanyName,");
            strSql.Append("IsShow=@IsShow,");
            strSql.Append("DefRole=@DefRole");
            strSql.Append(" where RoleId=@RoleId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RoleId", DbType.Guid, model.RoleId);
            db.AddInParameter(dbCommand, "RoleName", DbType.AnsiString, model.RoleName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "IsShow", DbType.Boolean, model.IsShow);
            db.AddInParameter(dbCommand, "DefRole", DbType.Boolean, model.DefRole);
            db.ExecuteNonQuery(dbCommand);

        }

        


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RoleId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Base_Permit_RoleDefine ");
            strSql.Append(" where RoleId=@RoleId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RoleId", DbType.Guid, RoleId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine GetModel(Guid RoleId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RoleId,RoleName,CityId,CityName,CompanyId,CompanyName,IsShow,DefRole ");
            strSql.Append(" from Base_Permit_RoleDefine ");
            strSql.Append(" where RoleId=@RoleId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RoleId", DbType.Guid, RoleId);
            Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RoleId,RoleName,CityId,CityName,CompanyId,CompanyName,IsShow,DefRole ");
            strSql.Append(" from Base_Permit_RoleDefine ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" Order by CityName,CompanyName,RoleName ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RoleId,RoleName,CityId,CityName,CompanyId,CompanyName,IsShow,DefRole ");
            strSql.Append(" from Base_Permit_RoleDefine ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" Order by CityName,CompanyName,RoleName ");
            List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine> list = new List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine model = new Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine();
            object ojb;
            ojb = dataReader["RoleId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RoleId = new Guid(ojb.ToString());
            }
            model.RoleName = dataReader["RoleName"].ToString();
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            model.CityName = dataReader["CityName"].ToString();
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.CompanyName = dataReader["CompanyName"].ToString();
            ojb = dataReader["IsShow"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsShow = (bool)ojb;
            }
            ojb = dataReader["DefRole"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DefRole = (bool)ojb;
            }
            return model;
        }

        /// <summary>
        /// 年假使用情况
        /// </summary>
        public static DataSet GetPaginationData(string TableNm, string strSelect, string TableFieldList, string strWhere, string strOrder, int intCurPage, int intRowCnt, Boolean bDoCount)
        {

            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, intCurPage);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, intRowCnt);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, strOrder);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableNm);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, strSelect);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, bDoCount);
            return db.ExecuteDataSet(dbCommand);
        }
        #endregion  成员方法
    }
}

